package thread;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/27
 * Time:19:25
 * 使用interrupt来终止线程
 **/
public class ThreadDemo12 {
    public static void main(String[] args) throws InterruptedException {
        //转账线程
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(!Thread.interrupted()){//判断线程的终止状态
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        System.out.println("终止状态"+Thread.currentThread().isInterrupted());
                        break;
                    }
                    System.out.println("正在转账");
                }
                System.out.println("终止线程");
            }
        });
        t1.start();
        Thread.sleep(310);

        System.out.println("有内鬼，终止交易");
        t1.interrupt();//终止线程
    }
}
